#include "stdafx.h"
#include "RenderTargetManager.h"
#include "GraphicsManager.h"

RenderTargetManager::RenderTargetManager(void)
{
	memset(renderTargets, 0, sizeof(ID3D11RenderTargetView*) * D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT);
	memset(shaderResources, 0, sizeof(ID3D11ShaderResourceView*) * MAX_TEXTURES);
}

RenderTargetManager::~RenderTargetManager(void)
{
}

void RenderTargetManager::Apply(void)
{
	GraphicsManager* pGraphics = GraphicsManager::GetInstance();
	ID3D11RenderTargetView* views[D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT];
	for (unsigned int i = 0; i < D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT; i++)
	{
		if (renderTargets[i])
			views[i] = renderTargets[i]->getDXRenderTarget();
		else
			views[i] = nullptr;
	}

	pGraphics->getDeviceContext()->PSSetShaderResources(0, MAX_TEXTURES, shaderResources);
	pGraphics->getDeviceContext()->OMSetRenderTargets(D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT, views, depthBuffer->getStencilView());
}

void RenderTargetManager::UnbindAll(void)
{
	GraphicsManager* pGraphics = GraphicsManager::GetInstance();

	ZeroMemory(renderTargets, sizeof(renderTargets));

	pGraphics->getDeviceContext()->OMSetRenderTargets(D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT, (ID3D11RenderTargetView**)renderTargets, nullptr);
}

void RenderTargetManager::SetRenderTarget(RenderTarget* rt, unsigned int slot, bool bindNow)
{
	renderTargets[slot] = rt;
	if (bindNow)
		Apply();
}